import * as Blockly from 'blockly/core';

////////////////////模拟输出////////////////////
export const ke_a_Write = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_val = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC);
    //generator.setups_['setup_output_'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
    var code = 'analogWrite(' + dropdown_pin + ',' + dropdown_val + ');\n'
    return code;
};

////////////////////红黄绿蓝白LED////////////////////
export const ke_led_r = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue('STAT');
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
    return code;
};

export const ke_led_g = ke_led_r;
export const ke_led_b = ke_led_r;
export const ke_led_y = ke_led_r;
export const ke_led_w = ke_led_r;
export const ke_led4 = ke_led_r;
export const ke_jg = ke_led_r;
export const ke_qcd = ke_led_r;


////////////////////双色LED////////////////////
export const ke_dual = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var dropdown_stat1 = this.getFieldValue('STAT1');
    var dropdown_stat2 = this.getFieldValue('STAT2');
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin1 + ',' + dropdown_stat1 + ');\ndigitalWrite(' + dropdown_pin2 + ',' + dropdown_stat2 + ');\n'
    return code;
};

////////////////////RGB01////////////////////
export const ke_rgb1_1 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var dropdown_pin3 = generator.valueToCode(this, 'PIN3', generator.ORDER_ATOMIC);
    var dropdown_stat1 = this.getFieldValue('STAT1');
    var dropdown_stat2 = this.getFieldValue('STAT2');
    var dropdown_stat3 = this.getFieldValue('STAT3');
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';
    generator.setups_['setup_output_' + dropdown_pin3] = 'pinMode(' + dropdown_pin3 + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin1 + ',' + dropdown_stat1 + ');\ndigitalWrite(' + dropdown_pin2 + ',' + dropdown_stat2 + ');\ndigitalWrite(' + dropdown_pin3 + ',' + dropdown_stat3 + ');\n'
    return code;
};


//////////////////////////RGB//////////////////////////////
export const ke_rgb1_2 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'R', generator.ORDER_ATOMIC);
    var red_pwm = generator.valueToCode(this, 'r', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';

    var dropdown_pin2 = generator.valueToCode(this, 'G', generator.ORDER_ATOMIC);
    var green_pwm = generator.valueToCode(this, 'g', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';

    var dropdown_pin3 = generator.valueToCode(this, 'B', generator.ORDER_ATOMIC);
    var blue_pwm = generator.valueToCode(this, 'b', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin3] = 'pinMode(' + dropdown_pin3 + ', OUTPUT);';

    var code = 'analogWrite(' + dropdown_pin1 + ',255-' + red_pwm + ');\nanalogWrite(' + dropdown_pin2 + ',255-' + green_pwm + ');\nanalogWrite(' + dropdown_pin3 + ',255-' + blue_pwm + ');\n'
    return code;
};

////////////////////RGB2////////////////////
export const ke_rgb2_1 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var dropdown_pin3 = generator.valueToCode(this, 'PIN3', generator.ORDER_ATOMIC);
    var dropdown_stat1 = this.getFieldValue('STAT1');
    var dropdown_stat2 = this.getFieldValue('STAT2');
    var dropdown_stat3 = this.getFieldValue('STAT3');
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';
    generator.setups_['setup_output_' + dropdown_pin3] = 'pinMode(' + dropdown_pin3 + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin1 + ',' + dropdown_stat1 + ');\ndigitalWrite(' + dropdown_pin2 + ',' + dropdown_stat2 + ');\ndigitalWrite(' + dropdown_pin3 + ',' + dropdown_stat3 + ');\n'
    return code;
};


//////////////////////////RGB2//////////////////////////////
export const ke_rgb2_2 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'R', generator.ORDER_ATOMIC);
    var red_pwm = generator.valueToCode(this, 'r', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';

    var dropdown_pin2 = generator.valueToCode(this, 'G', generator.ORDER_ATOMIC);
    var green_pwm = generator.valueToCode(this, 'g', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';

    var dropdown_pin3 = generator.valueToCode(this, 'B', generator.ORDER_ATOMIC);
    var blue_pwm = generator.valueToCode(this, 'b', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin3] = 'pinMode(' + dropdown_pin3 + ', OUTPUT);';

    var code = 'analogWrite(' + dropdown_pin1 + ',' + red_pwm + ');\nanalogWrite(' + dropdown_pin2 + ',' + green_pwm + ');\nanalogWrite(' + dropdown_pin3 + ',' + blue_pwm + ');\n'
    return code;
};

////////////////////蜂鸣器////////////////////
export const ke_w_buzzer1 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue('STAT');
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
    return code;
};

export const ke_w_buzzer2 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    var code = "tone(" + dropdown_pin + "," + fre + ");\n";
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    return code;
};

export const ke_w_buzzer3 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    var dur = generator.valueToCode(this, 'DURATION',
        generator.ORDER_ASSIGNMENT) || '0';
    var code = "tone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    return code;
};

////////////////////蜂鸣器//////////////////////
export const ke_tone01 = function (_, generator) {
    var code = this.getFieldValue('STAT');
    return [code, generator.ORDER_ATOMIC];
};

export const ke_buzzer = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    generator.setups_['setup_output'] = 'pinMode(' + dropdown_pin + ', OUTPUT);';

    var dropdown_type = this.getFieldValue('beat1');

    var code = 'tone(' + dropdown_pin + ',' + fre + ');\ndelay(' + dropdown_type + ');\n';
    /*if(window.isNaN(dropdown_pin)){
       code = code+'pinMode('+dropdown_pin+', OUTPUT);\n';
    }else{
       generator.setups_['setup_output_'+dropdown_pin] = 'pinMode('+dropdown_pin+', OUTPUT);';
    }*/
    //code += "tone("+dropdown_pin+","+fre+");\n";
    return code;
};

export const ke_controls_tone2 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    var dur = generator.valueToCode(this, 'DURATION',
        generator.ORDER_ASSIGNMENT) || '0';
    var code = "";
    if (window.isNaN(dropdown_pin)) {
        code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
    } else {
        generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    }
    code += "tone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
    return code;
};

//////////////////////////music///////////////////////////
export const ke_music = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_music'] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    generator.definitions_['include_birthday'] = 'void birthday()\n{\n  tone(' + dropdown_pin + ',294);\n  delay(250);\n  tone(' + dropdown_pin + ',440);\n  delay(250);\n  tone(' + dropdown_pin + ',392);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(250);\n  tone(' + dropdown_pin + ',494);\n  delay(500);\n  tone(' + dropdown_pin + ',392);\n  delay(250);\n  tone(' + dropdown_pin + ',440);\n  delay(250);\n  tone(' + dropdown_pin + ',392);\n  delay(250);\n  tone(' + dropdown_pin + ',587);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(500);\n  tone(' + dropdown_pin + ',392);\n  delay(250);\n  tone(' + dropdown_pin + ',784);\n  delay(250);\n  tone(' + dropdown_pin + ',659);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(250);\n  tone(' + dropdown_pin + ',494);\n  delay(250);\n  tone(' + dropdown_pin + ',440);\n  delay(250);\n  tone(' + dropdown_pin + ',698);\n  delay(375);\n  tone(' + dropdown_pin + ',659);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(250);\n  tone(' + dropdown_pin + ',587);\n  delay(250);\n  tone(' + dropdown_pin + ',532);\n  delay(500);\n}\n';
    generator.definitions_['include_tone'] = '//tone\n#define D0 -1\n#define D1 262\n#define D2 293\n#define D3 329\n#define D4 349\n#define D5 392\n#define D6 440\n#define D7 494\n#define M1 523\n#define M2 586\n#define M3 658\n#define M4 697\n#define M5 783\n#define M6 879\n#define M7 987\n#define H1 1045\n#define H2 1171\n#define H3 1316\n#define H4 1393\n#define H5 1563\n#define H6 1755\n#define H7 1971\n\n#define WHOLE 1\n#define HALF 0.5\n#define QUARTER 0.25\n#define EIGHTH 0.25\n#define SIXTEENTH 0.625\n ';

    generator.definitions_['include_tune'] = '\nint tune[]= \n{\n  M3,M3,M4,M5,\n  M5,M4,M3,M2,\n  M1,M1,M2,M3,\n  M3,M2,M2,\n  M3,M3,M4,M5,\n  M5,M4,M3,M2,\n  M1,M1,M2,M3,\n  M2,M1,M1,\n  M2,M2,M3,M1,\n  M2,M3,M4,M3,M1,\n  M2,M3,M4,M3,M2,\n  M1,M2,D5,D0,\n  M3,M3,M4,M5,\n  M5,M4,M3,M4,M2,\n  M1,M1,M2,M3,\n  M2,M1,M1\n};';
    generator.definitions_['include_durt'] = '\nfloat durt[]= \n {\n  1,1,1,1,\n  1,1,1,1,\n  1,1,1,1,\n  1+0.5,0.5,1+1,\n  1,1,1,1,\n  1,1,1,1,\n  1,1,1,1,\n  1+0.5,0.5,1+1,\n  1,1,1,1,\n  1,0.5,0.5,1,1,\n  1,0.5,0.5,1,1,\n  1,1,1,1,\n  1,1,1,1,\n  1,1,1,0.5,0.5,\n  1,1,1,1,\n  1+0.5,0.5,1+1,\n };';
    generator.definitions_['include_io1'] = '\n int length;\n int tonepin=' + dropdown_pin + '; \n';
    generator.definitions_['include_Ode_to_Joy'] = 'void Ode_to_Joy()\n{\n  for(int x=0;x<length;x++)\n  {\n    tone(tonepin,tune[x]);\n    delay(300*durt[x]);   \n  }\n}\n';

    generator.setups_['setup_output_Ode'] = 'length=sizeof(tune)/sizeof(tune[0]);\n';
    var dropdown_type = this.getFieldValue('play');

    var code = '';
    if (dropdown_type == "Birthday") code += 'birthday();\n';
    if (dropdown_type == "Ode to Joy") code += 'Ode_to_Joy();\n';
    //if (dropdown_type == "City of Sky") code += 'digitalRead(8)';
    return code;
    //return [code, generator.ORDER_ATOMIC];
};

export const ke_notone = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_output'] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = '';
    code += "noTone(" + dropdown_pin + ");\n";
    return code;
};



////////////////////继电器////////////////////
export const ke_relay = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue('STAT');
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
    return code;
};


///////////////////震动马达////////////////////
export const ke_v_motor = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue('STAT');
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    var code = 'digitalWrite(' + dropdown_pin + ',' + dropdown_stat + ');\n'
    return code;
};

///////////////////////扬声器模块////////////////////////////

export const ke_speaker = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY', generator.ORDER_ASSIGNMENT) || '0';
    var code = "tone(" + dropdown_pin + "," + fre + ");\n";
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    return code;
};

export const ke_speaker2 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var fre = generator.valueToCode(this, 'FREQUENCY',
        generator.ORDER_ASSIGNMENT) || '0';
    var dur = generator.valueToCode(this, 'DURATION',
        generator.ORDER_ASSIGNMENT) || '0';
    var code = "tone(" + dropdown_pin + "," + fre + "," + dur + ");\n";
    generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    return code;
};

// Notone 结束声音
export const ke_speaker_notone = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var code = '';
    if (window.isNaN(dropdown_pin)) {
        code = code + 'pinMode(' + dropdown_pin + ', OUTPUT);\n';
    } else {
        generator.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', OUTPUT);';
    }
    code += "noTone(" + dropdown_pin + ");\n";
    return code;
};
////////////////////电机////////////////////
export const ke_motor = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_stat1 = this.getFieldValue('STAT1');
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';

    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var dropdown_stat2 = this.getFieldValue('STAT2');
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';

    var code = 'digitalWrite(' + dropdown_pin1 + ',' + dropdown_stat1 + ');\ndigitalWrite(' + dropdown_pin2 + ',' + dropdown_stat2 + ');\n'
    return code;
};

export const ke_motor001 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var data_a = this.getFieldValue('STAT1');
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';

    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var value_pwmb = generator.valueToCode(this, 'pwmb', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';

    var code = 'digitalWrite(' + dropdown_pin1 + ',' + data_a + ');\nanalogWrite(' + dropdown_pin2 + ',' + value_pwmb + ');\n'
    return code;
};


export const ke_motor2 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_stat1 = this.getFieldValue('STAT1');
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';

    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var dropdown_stat2 = this.getFieldValue('STAT2');
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';

    var code = 'digitalWrite(' + dropdown_pin1 + ',' + dropdown_stat1 + ');\ndigitalWrite(' + dropdown_pin2 + ',' + dropdown_stat2 + ');\n'
    return code;
};

export const ke_motor002 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var data_a = this.getFieldValue('STAT1');
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';

    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var value_pwmb = generator.valueToCode(this, 'pwmb', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', OUTPUT);';

    var code = 'digitalWrite(' + dropdown_pin1 + ',' + data_a + ');\nanalogWrite(' + dropdown_pin2 + ',' + value_pwmb + ');\n'
    return code;
};


////////////////////舵机////////////////////
export const ke_servo = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var value_degree = generator.valueToCode(this, 'angle', generator.ORDER_ATOMIC);
    //value_degree = value_degree.replace('(','').replace(')','')
    var delay_time = generator.valueToCode(this, 'time', generator.ORDER_ATOMIC) || '0'
    //delay_time = delay_time.replace('(','').replace(')','');

    generator.definitions_['include_Servo'] = '#include <Servo.h>';
    generator.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';';
    generator.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');';

    var code = 'servo_' + dropdown_pin + '.write(' + value_degree + ');\n' + 'delay(' + delay_time + ');\n';
    return code;
};

export const ke_servo_r = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);

    generator.definitions_['include_Servo'] = '#include <Servo.h>';
    generator.definitions_['var_servo' + dropdown_pin] = 'Servo servo_' + dropdown_pin + ';';
    generator.setups_['setup_servo_' + dropdown_pin] = 'servo_' + dropdown_pin + '.attach(' + dropdown_pin + ');';

    var code = 'servo_' + dropdown_pin + '.read()';
    return [code, generator.ORDER_ATOMIC];
};


//////////////////////////数字传感器////////////////////////////////

export const ke_ir_g = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'digitalRead(' + dropdown_pin + ')';
    return [code, generator.ORDER_ATOMIC];
};
/////////////////火焰传感器////////////////
export const ke_flame = ke_ir_g;


/////////////////霍尔传感器////////////////
export const ke_hall = ke_ir_g;


/////////////////碰撞传感器////////////////
export const ke_crash = ke_ir_g;


/////////////////按键传感器////////////////
export const ke_button = ke_ir_g;


/////////////////自锁按键传感器////////////////
export const ke_sl_button = ke_ir_g;

/////////////////电容触摸传感器////////////////
export const ke_tuoch = ke_ir_g;

/////////////////敲击传感器////////////////
export const ke_knock = ke_ir_g;


/////////////////倾斜传感器////////////////
export const ke_tilt = ke_ir_g;


/////////////////振动传感器////////////////
export const ke_shake = ke_ir_g;


/////////////////干簧管传感器////////////////
export const ke_reed_s = ke_ir_g;


/////////////////循迹传感器////////////////
export const ke_track = ke_ir_g;


/////////////////避障传感器////////////////
export const ke_avoid = ke_ir_g;

/////////////////光折断传感器////////////////
export const ke_light_b = ke_ir_g;

/////////////////烟雾数字传感器/////////////////
export const ke_smoke_D = ke_ir_g;

/////////////////////酒精数字传感器///////////////
export const ke_alcohol_D = ke_ir_g;


//////////////////////模拟传感器/////////////////////////
export const ke_sound = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'analogRead(' + dropdown_pin + ')';
    return [code, generator.ORDER_ATOMIC];
};

////////////////////LM35温度////////////////////
export const ke_lm35 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'analogRead(' + dropdown_pin + ')*0.488';
    return [code, generator.ORDER_ATOMIC];
};


export const ke_analog_t = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);

    generator.definitions_['include_math'] = '#include <math.h>';

    generator.definitions_['func_temp'] = 'double Thermister(int RawADC) {\n  double Temp;\n  Temp = log(((10240000/RawADC) - 10000));\n  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );\n  Temp = Temp - 273.15; // Convert Kelvin to Celcius\n  return Temp;\n}\n';

    var code = 'Thermister(analogRead(' + dropdown_pin + '))';
    return [code, generator.ORDER_ATOMIC];
};


////////////////////光线////////////////////

export const ke_light = ke_sound;

////////////////////紫外线////////////////////

export const ke_UV = ke_sound;

////////////////////压电////////////////////

export const ke_Piezo = ke_sound;

////////////////////水位////////////////////

export const ke_water = ke_sound;

////////////////////土壤////////////////////

export const ke_soil = ke_sound;

////////////////////电位器////////////////////

export const ke_potentiometer = ke_sound;

////////////////////滑动电位器////////////////////


export const ke_slide_potentiometer = ke_sound;

////////////////////TEMT6000光线////////////////////

export const ke_temt6000 = ke_sound;

////////////////////水蒸气////////////////////
///
export const ke_steam = ke_sound;

//////////////////// 压力传感器////////////////////
export const ke_film_p = ke_sound;

////////////////////烟雾////////////////////

export const ke_smoke = ke_sound;

////////////////////酒精////////////////////
export const ke_alcohol = ke_sound;

////////////////////18B20温度////////////////////
// export const ke_18b20 = ke_sound;

////////////////////温度湿度////////////////////
// export const ke_dht11 = ke_sound;

////////////////////高度////////////////////
// export const ke_bmp180 = ke_sound;

////////////////////火焰////////////////////
export const ke_fire2 = ke_sound;


////////////////////电压////////////////////

export const Voltage = ke_sound;

////////////////////电流////////////////////

export const Current = ke_sound;
///////////////////////////////////////////////////
///////////////////其他传感器/////////////////////////
////////////////////////////////////////////////////

////////////////ADKey///////////////////////
export const ke_ADKey = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'analogRead(' + dropdown_pin + ')';
    return [code, generator.ORDER_ATOMIC];
};


///////////////////////超声波//////////////////////
export const ke_sr04 = function (_, generator) {
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    generator.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';
    generator.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', INPUT);';
    var funcName = 'checkdistance_' + dropdown_pin1 + '_' + dropdown_pin2;
    var code = 'float' + ' ' + funcName + '() {\n'
        + '  digitalWrite(' + dropdown_pin1 + ', LOW);\n' + '  delayMicroseconds(2);\n'
        + '  digitalWrite(' + dropdown_pin1 + ', HIGH);\n' + '  delayMicroseconds(10);\n'
        + '  digitalWrite(' + dropdown_pin1 + ', LOW);\n'
        + '  float distance = pulseIn(' + dropdown_pin2 + ', HIGH) / 58.00;\n'
        + '  delay(10);\n' + '  return distance;\n'
        + '}\n';
    generator.definitions_[funcName] = code;
    return [funcName + '()', generator.ORDER_ATOMIC];
};

///////////////////18B20////////////////////////////
export const ke_18b20 = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var unit = this.getFieldValue('UNIT');
    generator.definitions_['include_OneWire'] = '#include <OneWire.h>';
    generator.definitions_['include_DallasTemperature'] = '#include <DallasTemperature.h>';
    generator.definitions_['var_OneWire_oneWire_' + dropdown_pin] = 'OneWire oneWire_' + dropdown_pin + '(' + dropdown_pin + ');';
    generator.definitions_['var_DallasTemperature_sensors_' + dropdown_pin] = 'DallasTemperature sensors_' + dropdown_pin + '(&oneWire_' + dropdown_pin + ');';
    generator.definitions_['var_DeviceAddress_insideThermometer'] = 'DeviceAddress insideThermometer;';
    generator.setups_['setup_sensors_' + dropdown_pin + '_getAddress'] = 'sensors_' + dropdown_pin + '.getAddress(insideThermometer, 0);';
    generator.setups_['setup_sensors_' + dropdown_pin + '_setResolution'] = 'sensors_' + dropdown_pin + '.setResolution(insideThermometer, 9);';
    var funcName = 'ds18b20_' + dropdown_pin + '_getTemp';
    var code = 'float' + ' ' + funcName + '(int w) {\n'
        + '  sensors_' + dropdown_pin + '.requestTemperatures();\n'
        + '  if(w==0) {\nreturn sensors_' + dropdown_pin + '.getTempC(insideThermometer);\n}\n'
        + '  else {\nreturn sensors_' + dropdown_pin + '.getTempF(insideThermometer);\n}\n'
        + '}\n';
    generator.definitions_[funcName] = code;
    return ['ds18b20_' + dropdown_pin + '_getTemp(' + unit + ')', generator.ORDER_ATOMIC];
}

//////////////////////////DHT11///////////////////////////
export const ke_dht11 = function (_, generator) {
    var sensor_type = this.getFieldValue('TYPE');
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var what = this.getFieldValue('WHAT');
    generator.definitions_['include_dht'] = '#include <dht.h>';
    generator.definitions_['var_dht_' + dropdown_pin] = 'dht myDHT_' + dropdown_pin + ';';
    var funcName = 'dht_' + dropdown_pin + '_get' + what;
    var code = 'int' + ' ' + funcName + '() {\n'
        + '  int chk = myDHT_' + dropdown_pin + '.read' + sensor_type + '(' + dropdown_pin + ');\n'
        + '  int value = myDHT_' + dropdown_pin + '.' + what + ';\n'
        + '  return value;\n'
        + '}\n';
    generator.definitions_[funcName] = code;
    return [funcName + '()', generator.ORDER_ATOMIC];
}

export const ke_DHT = function (_, generator) {
    var sensor_type = this.getFieldValue('TYPE');
    var dropdown_pin = this.getFieldValue('PIN');
    var what = this.getFieldValue('WHAT');
    generator.definitions_['include_DHT'] = '#include <DHT.h>';
    generator.definitions_['var_declare_dht' + dropdown_pin] = 'DHT dht' + dropdown_pin + '(' + dropdown_pin + ', ' + sensor_type + ');'
    generator.setups_['DHT_SETUP' + dropdown_pin] = ' dht' + dropdown_pin + '.begin();';
    var code;
    if (what == "temperature")
        code = 'dht' + dropdown_pin + '.readTemperature()'
    else
        code = 'dht' + dropdown_pin + '.readHumidity()'
    return [code, generator.ORDER_ATOMIC];
}


/////////////////////////BMP280////////////////////////////
export const ke_bmp280 = function (_, generator) {
    generator.definitions_['include_BMP280'] = '#include <Adafruit_BMP280.h>';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_spi'] = '#include <SPI.h>';
    generator.definitions_['include_Sensor'] = '#include <Adafruit_Sensor.h>';

    generator.definitions_['include_SCK'] = '#define BMP_SCK 13';
    generator.definitions_['include_MISO'] = '#define BMP_MISO 12';
    generator.definitions_['include_MOSI'] = '#define BMP_MOSI 11';
    generator.definitions_['include_CS'] = '#define BMP_CS 10';

    generator.definitions_['include_bme'] = 'Adafruit_BMP280 bme; // I2C';

    generator.definitions_['readPressure'] = 'int readPressure()\n{\n long int  pressure = 0;\n  int temp = 0;\n  temp = bme.readTemperature();\n  pressure = bme.readPressure();\n  return pressure;\n}\n';

    generator.definitions_['readAltitude'] = 'int readAltitude()\n{\nint temp = 0;\n  temp = bme.readTemperature();\n  int altitude1 = 0;\n  altitude1 = (101325-bme.readPressure()) * 0.09;\n  return altitude1;\n}\n';

    generator.setups_['setup_!bme'] = 'bme.begin();\n';

    var dropdown_type = this.getFieldValue('BMP280_PIN');
    var code = '';

    if (dropdown_type == "T") code += 'bme.readTemperature()';
    if (dropdown_type == "P") code += 'bme.readPressure();bme.readTemperature()';
    if (dropdown_type == "A") code += 'readAltitude()';

    return [code, generator.ORDER_ATOMIC];
};

//BME280读取
export const ke_BMP280_READ = function (_, generator) {
    var TYPE = this.getFieldValue('TYPE');
    var address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_SPI'] = '#include <SPI.h>';
    generator.definitions_['include_Adafruit_Sensor'] = '#include <Adafruit_Sensor.h>';
    if (TYPE == "bme") {
        generator.definitions_['include_Adafruit_BME280'] = '#include <Adafruit_BME280.h>';
        generator.definitions_['var_declare_Adafruit_BME280'] = 'Adafruit_BME280 bme;';

    }
    else {
        generator.definitions_['include_Adafruit_BME280'] = '#include <Adafruit_BMP280.h>';
        generator.definitions_['var_declare_Adafruit_BME280'] = 'Adafruit_BMP280 bmp;';
    }
    generator.setups_['setup_status'] = 'unsigned status;\n  status = ' + TYPE + '.begin(' + address + ');';
    generator.definitions_['include_SEALEVELPRESSURE_HPA'] = '#define SEALEVELPRESSURE_HPA (1013.25)';
    var code = this.getFieldValue('BME_TYPE');
    return [TYPE + "." + code, generator.ORDER_ATOMIC];
};

//传感器-重力感应块-获取数据
export const ke_adxl345 = function (_, generator) {
    generator.definitions_['include_ADXL345'] = '#include <ADXL345.h>';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_I2Cdev'] = '#include <I2Cdev.h>';
    generator.definitions_['var_ADXL345'] = 'ADXL345 accel;';
    generator.setups_['setup_ADXL345-A'] = 'Wire.begin();';
    generator.setups_['setup_ADXL345-B'] = 'accel.initialize();';
    var dropdown_type = this.getFieldValue('ADXL345_PIN');
    var code = '';
    if (dropdown_type == "xa") code += 'accel.X_angle()';
    if (dropdown_type == "ya") code += 'accel.Y_angle()';
    if (dropdown_type == "x") code += 'accel.getAccelerationX()';
    if (dropdown_type == "y") code += 'accel.getAccelerationY()';
    if (dropdown_type == "z") code += 'accel.getAccelerationZ()';
    return [code, generator.ORDER_ATOMIC];
};



//传感器-MPU6050-获取数据
export const ke_MPU6050 = function (_, generator) {
    generator.definitions_['includeMPU6050'] = '#include <MPU6050.h>';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['var_declare_MPU6050'] = 'MPU6050 mpu;';
    generator.setups_['setup_ngyro'] = 'mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G);';
    var MPU6050_TYPE = this.getFieldValue('MPU6050_TYPE');
    var code = MPU6050_TYPE;
    return [code, generator.ORDER_ATOMIC];
};
//传感器-MPU6050-更新数据
export const ke_MPU6050_update = function () {
    var code = 'Vector normAccel = mpu.readNormalizeAccel();\nVector normGyro = mpu.readNormalizeGyro();\n';
    return code;
};


///////////////////////BMP180温度大气压/////////////////////////////
export const ke_bmp180 = function (_, generator) {
    generator.definitions_['define_bmp180'] = '#define BMP180ADD 0xEE>>1';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['char_OSS'] = 'unsigned char OSS;';
    generator.definitions_['int'] = 'int ac1,ac2,ac3,b1,b2,mb,mc,md;';
    generator.definitions_['unsight_int'] = 'unsigned int ac4,ac5,ac6;';
    generator.definitions_['float'] = 'float temperature;';
    generator.definitions_['double'] = 'double pressure,pressure2,altitude;';
    generator.definitions_['long'] = 'long b5;';

    //generator.definitions_['calculate'] = 'void calculate()\n{\n  temperature = bmp180GetTemperature(bmp180ReadUT());\ntemperature = temperature*0.1;\npressure = bmp180GetPressure(bmp180ReadUP());\npressure2 = pressure/101325;\npressure2 = pow(pressure2,0.19029496);\naltitude = 44330*(1-pressure2);\n}';

    //generator.definitions_['show'] = 'void show()\n{\n  Serial.print("Temperature: ");\nSerial.print(temperature, 1);\nSerial.println(" C");\nSerial.print("Pressure: ");\nSerial.print(pressure, 0);\nSerial.println(" Pa");\nSerial.print("altitude:");\nSerial.print(altitude);\nSerial.println("m");\n}';

    generator.definitions_['BMP180start'] = 'void BMP180start()\n{\nac1 = bmp180ReadDate(0xAA);\nac2 = bmp180ReadDate(0xAC);\nac3 = bmp180ReadDate(0xAE);\nac4 = bmp180ReadDate(0xB0);\nac5 = bmp180ReadDate(0xB2);\nac6 = bmp180ReadDate(0xB4);\nb1  = bmp180ReadDate(0xB6);\nb2  = bmp180ReadDate(0xB8);\nmb  = bmp180ReadDate(0xBA);\nmc  = bmp180ReadDate(0xBC);\nmd  = bmp180ReadDate(0xBE);\n}';

    generator.definitions_['bmp180GetTemperature'] = 'short bmp180GetTemperature(unsigned int ut)\n{\nlong x1, x2;\nx1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;\nx2 = ((long)mc << 11)/(x1 + md);\nb5 = x1 + x2;\nreturn ((b5 + 8)>>4);\n}';

    generator.definitions_['bmp180GetPressure'] = 'long bmp180GetPressure(unsigned long up)\n{\nlong x1, x2, x3, b3, b6, p;\nunsigned long b4, b7;\nb6 = b5 - 4000;\nx1 = (b2 * (b6 * b6)>>12)>>11;\nx2 = (ac2 * b6)>>11;\nx3 = x1 + x2;\nb3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;\nx1 = (ac3 * b6)>>13;\nx2 = (b1 * ((b6 * b6)>>12))>>16;\nx3 = ((x1 + x2) + 2)>>2;\nb4 = (ac4 * (unsigned long)(x3 + 32768))>>15;\nb7 = ((unsigned long)(up - b3) * (50000>>OSS));\nif (b7 < 0x80000000)\n  p = (b7<<1)/b4;\nelse\n  p = (b7/b4)<<1;\nx1 = (p>>8) * (p>>8);\nx1 = (x1 * 3038)>>16;\nx2 = (-7357 * p)>>16;\np += (x1 + x2 + 3791)>>4;\nreturn p;\n}';

    generator.definitions_['bmp180Read'] = 'int bmp180Read(unsigned char address)\n{\nunsigned char data;\nWire.beginTransmission(BMP180ADD);\nWire.write(address);\nWire.endTransmission();\nWire.requestFrom(BMP180ADD, 1);\nwhile(!Wire.available());\nreturn Wire.read();\n}';

    generator.definitions_['bmp180ReadDate'] = 'int bmp180ReadDate(unsigned char address)\n{\nunsigned char msb, lsb;\nWire.beginTransmission(BMP180ADD);\nWire.write(address);\nWire.endTransmission();\nWire.requestFrom(BMP180ADD, 2);\nwhile(Wire.available()<2);\nmsb = Wire.read();\nlsb = Wire.read();\nreturn (int) msb<<8 | lsb;\n}';

    generator.definitions_['bmp180ReadUT'] = 'unsigned int bmp180ReadUT()\n{\nunsigned int ut;\nWire.beginTransmission(BMP180ADD);\nWire.write(0xF4);\nWire.write(0x2E);\nWire.endTransmission();\ndelay(5);\nut = bmp180ReadDate(0xF6);\nreturn ut;\n}';

    generator.definitions_['bmp180ReadUP'] = 'unsigned long bmp180ReadUP()\n{\nunsigned char msb, lsb, xlsb;\nunsigned long up = 0;\nWire.beginTransmission(BMP180ADD);\nWire.write(0xF4);\nWire.write(0x34 + (OSS<<6));\nWire.endTransmission();\ndelay(2 + (3<<OSS));\nWire.beginTransmission(BMP180ADD);\nWire.write(0xF6);\nWire.endTransmission();\nWire.requestFrom(BMP180ADD, 3);\nwhile(Wire.available() < 3);\nmsb = Wire.read();\nlsb = Wire.read();\nxlsb = Wire.read();\nup = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);\nreturn up;\n}';

    generator.definitions_['temperature1'] = 'float temperature1()\n{\ntemperature = bmp180GetTemperature(bmp180ReadUT());\ntemperature = temperature*0.1;\nreturn temperature;\n}';

    generator.definitions_['pressure1'] = 'double pressure1()\n{\nbmp180GetTemperature(bmp180ReadUT());\npressure = bmp180GetPressure(bmp180ReadUP());\nreturn pressure;\n}\n'

    generator.definitions_['altitude1'] = 'double altitude1()\n{\nbmp180GetTemperature(bmp180ReadUT());\npressure = bmp180GetPressure(bmp180ReadUP());\npressure2 = pressure/101325;\npressure2 = pow(pressure2,0.19029496);\naltitude = 44330*(1-pressure2);\nreturn altitude;\n}\n'

    generator.setups_['wire.begin'] = 'Wire.begin();';
    generator.setups_['OSS'] = 'OSS = 2;';
    generator.setups_['BMP180start'] = 'BMP180start();';


    var dropdown_type = this.getFieldValue('BMP180_PIN');
    var code = '';

    if (dropdown_type == "T") code += 'temperature1()';
    if (dropdown_type == "P") code += 'pressure1()';
    if (dropdown_type == "A") code += 'altitude1()';
    //return code;
    return [code, generator.ORDER_ATOMIC];
};

/////////////////////////////DS3231时钟/////////////////////////////
export const ke_ds3231 = function (_, generator) {
    var RTCName = this.getFieldValue('RTCName');
    var year = generator.valueToCode(this, "year", generator.ORDER_ATOMIC);
    var month = generator.valueToCode(this, "month", generator.ORDER_ATOMIC);
    var day = generator.valueToCode(this, "day", generator.ORDER_ATOMIC);
    var dow = generator.valueToCode(this, "dow", generator.ORDER_ATOMIC)
    var hour = generator.valueToCode(this, "hour", generator.ORDER_ATOMIC);
    var minute = generator.valueToCode(this, "minute", generator.ORDER_ATOMIC);
    var second = generator.valueToCode(this, "second", generator.ORDER_ATOMIC);


    generator.definitions_['include_DS3231_h'] = '#include <DS3231.h>';
    generator.definitions_['DS3231'] = 'DS3231  ' + RTCName + '(A4, A5);';

    generator.setups_['begin1'] = '' + RTCName + '.begin();';
    generator.setups_['setDOW'] = '' + RTCName + '.setDOW(' + dow + ');';
    generator.setups_['setTime'] = '' + RTCName + '.setTime(' + hour + ', ' + minute + ', ' + second + ');';
    generator.setups_['setDate'] = '' + RTCName + '.setDate(' + day + ', ' + month + ', ' + year + ');';

    var code = 'Serial.print(' + RTCName + '.getDOWStr());\nSerial.print(" ");\nSerial.print(' + RTCName + '.getDateStr());\nSerial.print(" -- ");\nSerial.println(' + RTCName + '.getTimeStr());\ndelay (1000);';

    return code;
};

/********************************************
TIME - 时钟模块
  - DS3231
  - DS1307
*********************************************/
// DS3231 real time clock  -- 设置时间
export const ke_ds3231_SetTime = function (_, generator) {
    generator.definitions_['define_i2c'] = '#include <Wire.h>';
    generator.definitions_['include_ds3231'] = '#include "RtcDS3231.h"';
    generator.definitions_['var_ds3231'] = 'RtcDS3231<TwoWire> Rtc(Wire);';
    generator.setups_['setup_init_rtc'] = 'Rtc.Begin();\n';
    var year = generator.valueToCode(this, 'year', generator.ORDER_ATOMIC);
    var month = generator.valueToCode(this, 'month', generator.ORDER_ATOMIC);
    var day = generator.valueToCode(this, 'day', generator.ORDER_ATOMIC);
    var hour = generator.valueToCode(this, 'hour', generator.ORDER_ATOMIC);
    var minute = generator.valueToCode(this, 'minute', generator.ORDER_ATOMIC);
    var second = generator.valueToCode(this, 'second', generator.ORDER_ATOMIC);
    generator.setups_['setup_set_time'] = 'Rtc.SetDateTime(RtcDateTime(' + year + ', ' + month + ', '
        + day + ', ' + hour + ', ' + minute + ', ' + second + '));\n';
    return '';
};

// DS3231 real time clock  -- 设置当前时间
export const ke_ds3231_SetTime2 = function (_, generator) {
    generator.definitions_['define_i2c'] = '#include <Wire.h>';
    generator.definitions_['include_ds3231'] = '#include "RtcDS3231.h"';
    generator.definitions_['var_ds3231'] = 'RtcDS3231<TwoWire> Rtc(Wire);';
    generator.setups_['setup_init_rtc'] = 'Rtc.Begin();\n';
    generator.setups_['setup_set_time'] = 'Rtc.SetDateTime(RtcDateTime(__DATE__, __TIME__));\n';
    return '';
};

// DS3231 real time clock  -- 获取
export const ke_ds3231_GetTime = function (_, generator) {
    var dropdown_type = this.getFieldValue('TIME_TYPE');
    var code = 'Rtc.GetDateTime().' + dropdown_type + '()';
    // return code;
    return [code, generator.ORDER_ATOMIC];
};

// DS1307 real time clock  -- 设置时间
export const ke_ds1307_SetTime = function (_, generator) {
    generator.definitions_['define_i2c'] = '#include <Wire.h>';
    generator.definitions_['include_ds1307'] = '#include "RtcDS1307.h"';
    generator.definitions_['var_ds1307'] = 'RtcDS1307<TwoWire> Rtc(Wire);';
    generator.setups_['setup_init_rtc'] = 'Rtc.Begin();\n  Rtc.SetIsRunning(true);\n';
    var year = generator.valueToCode(this, 'year', generator.ORDER_ATOMIC);
    var month = generator.valueToCode(this, 'month', generator.ORDER_ATOMIC);
    var day = generator.valueToCode(this, 'day', generator.ORDER_ATOMIC);
    var hour = generator.valueToCode(this, 'hour', generator.ORDER_ATOMIC);
    var minute = generator.valueToCode(this, 'minute', generator.ORDER_ATOMIC);
    var second = generator.valueToCode(this, 'second', generator.ORDER_ATOMIC);
    generator.setups_['setup_set_time'] = 'Rtc.SetDateTime(RtcDateTime(' + year + ', ' + month + ', '
        + day + ', ' + hour + ', ' + minute + ', ' + second + '));\n';
    return '';
};

// DS1307 real time clock  -- 设置当前时间
export const ke_ds1307_SetTime2 = function (_, generator) {
    generator.definitions_['define_i2c'] = '#include <Wire.h>';
    generator.definitions_['include_ds1307'] = '#include "RtcDS1307.h"';
    generator.definitions_['var_ds1307'] = 'RtcDS1307<TwoWire> Rtc(Wire);';
    generator.setups_['setup_init_rtc'] = 'Rtc.Begin();\n  Rtc.SetIsRunning(true);\n';
    generator.setups_['setup_set_time'] = 'Rtc.SetDateTime(RtcDateTime(__DATE__, __TIME__));\n';
    return '';
};

// DS1307 real time clock  -- 获取
export const ke_ds1307_GetTime = function (_, generator) {
    var dropdown_type = this.getFieldValue('TIME_TYPE');
    var code = 'Rtc.GetDateTime().' + dropdown_type + '()';
    // return code;
    return [code, generator.ORDER_ATOMIC];
};



/////////////////旋转编码器--按钮////////////////////
export const ke_rotation_button = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'button', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    var code = 'digitalRead(' + dropdown_pin + ')';
    return [code, generator.ORDER_ATOMIC];
    //return code;
};


/////////////////旋转编码器--旋转////////////////////
export const ke_rotation_right = function (_, generator) {
    var variable0 = generator.variableDB_.getName(
        this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
    var dropdown_DAT = generator.valueToCode(this, 'DAT', generator.ORDER_ATOMIC);
    var dropdown_CLK = generator.valueToCode(this, 'CLK', generator.ORDER_ATOMIC);
    //var dropdown_SW = generator.valueToCode(this, 'button',generator.ORDER_ATOMIC);

    generator.definitions_['include_PinChangeInt.h'] = '#include <PinChangeInt.h>';
    //generator.definitions_['DAT'] = 'int DAT = A2; ';
    //generator.definitions_['CLK'] = 'int CLK = 2;';
    //generator.definitions_['BUTTON'] = 'int BUTTON = A3;';
    generator.definitions_['COUNT'] = 'int ' + variable0 + ' = 0;';
    //generator.definitions_['COUNT'] = 'int COUNT = 0;';

    generator.definitions_['RoteStateChanged()'] = 'int RoteStateChanged() \n{\nif  (digitalRead(' + dropdown_DAT + '))\n{\n' + variable0 + '++;\ndelay(20);\n}else              \n   {\n    ' + variable0 + '--;\n    delay(20);\n   }\nreturn(' + variable0 + '); \n}';
    //else\n{\nCOUNT--;\ndelay(20);\n}\n
    //generator.definitions_['RoteStateChanged'] = 'void RoteStateChanged() \n{\nif  (digitalRead('+dropdown_DAT+'))\n{\nCOUNT++;\ndelay(20);\n}\n';
    generator.setups_['setup_input_dropdown'] = 'pinMode(' + dropdown_CLK + ', INPUT);\n  digitalWrite(' + dropdown_CLK + ', HIGH);\n  pinMode(' + dropdown_DAT + ', INPUT);\n ';

    var code = 'attachPinChangeInterrupt(' + dropdown_CLK + ',RoteStateChanged , FALLING);\n';
    return code;
    //return [code, generator.ORDER_ATOMIC];
};

/////////////////////遥杆///////////////////////////
export const ke_joystick = function (_, generator) {
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    generator.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
    //var code = 'analogRead('+dropdown_pin+')';

    var dropdown_type = this.getFieldValue('joy');
    var code = '';
    if (dropdown_type == "Lx") code += 'analogRead(' + dropdown_pin + ')';
    if (dropdown_type == "Ly") code += 'analogRead(' + dropdown_pin + ')';
    //if (dropdown_type == "Lz") code += 'digitalRead('+dropdown_pin+')';
    //if (dropdown_type == "Rx") code += 'analogRead('+dropdown_pin+')';

    return [code, generator.ORDER_ATOMIC];
};
/////////////////遥杆///////////////
export const ke_joystick_btn = ke_ir_g;

///////////////////////////pca9685/////////////////
export const ke_pca9685 = function (_, generator) {

    var value_pin = generator.valueToCode(this, 'pin', generator.ORDER_ATOMIC);
    var value_pwm = generator.valueToCode(this, 'pwm', generator.ORDER_ATOMIC);

    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_PWMServoDriver'] = '#include <Adafruit_PWMServoDriver.h>';

    generator.definitions_['PWMServo'] = 'Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();';

    generator.definitions_['PWMangle'] = 'void servo_9g_write(uint8_t n,int Angle)\n{\n  double pulse = Angle;\n  pulse = pulse/100+0.5;\n  setServoPulse(n,pulse); \n}\n';

    generator.definitions_['setServoPulse'] = 'void setServoPulse(uint8_t n, double pulse) \n {\n  double pulselength;\n  pulselength = 1000000;   // 1,000,000 us per second\n  pulselength /= 50;   // 60 Hz\n  Serial.print(pulselength); Serial.println(" us per period"); \n  pulselength /= 4096;  // 12 bits of resolution\n  Serial.print(pulselength); Serial.println(" us per bit"); \n  pulse *= 1000;  // convert to us\n  pulse /= pulselength;\n  Serial.println(pulse);\n  pwm.setPWM(n, 0, pulse);\n}\n';

    //generator.definitions_['pulselen'] = 'uint16_t pulselen ='+value_pwm+';';

    generator.setups_['setup_begin'] = 'pwm.begin();\npwm.setPWMFreq(50);\ndelay(10);\n';

    var code = 'servo_9g_write(' + value_pin + ',' + value_pwm + ');\n';

    return code;
};

///////////////////////////红外测温传感器/////////////////

//初始化MLX90614红外测温传感器
export const mlx90614_init = function (_, generator) {
    var value_mlx90614_address = generator.valueToCode(this, 'mlx90614_address', generator.ORDER_ATOMIC);
    var text_mlx90614_name = 'MLX';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_Adafruit_MLX90614'] = '#include <Adafruit_MLX90614.h>';
    generator.definitions_['var_declare_MLX90614_' + text_mlx90614_name] = 'Adafruit_MLX90614 ' + text_mlx90614_name + ' = Adafruit_MLX90614(' + value_mlx90614_address + ');';
    generator.setups_['setup_MLX90614_' + text_mlx90614_name] = '' + text_mlx90614_name + '.begin();';
    var code = '';
    return code;
};
//MLX90614获取数据
export const mlx90614_get_data = function (_, generator) {
    var text_mlx90614_name = 'MLX';
    var dropdown_mlx90614_data = this.getFieldValue('mlx90614_data');
    var code = '' + text_mlx90614_name + '.' + dropdown_mlx90614_data + '()';
    return [code, generator.ORDER_ATOMIC];
};

//////////////////////////颜色传感器//////////////////////////

//颜色识别传感器库
export const TCS34725_Get_RGB = function (_, generator) {
    generator.definitions_['include_DFRobot_TCS34725'] = '#include <DFRobot_TCS34725.h>';
    generator.definitions_['var_DFRobot_TCS34725'] = 'DFRobot_TCS34725 tcs34725;\n';
    // generator.setups_['setup_DFRobot_TCS34725' ] = 'if (tcs34725.begin()) {\n  Serial.println("Found sensor");\n} \nelse { \nSerial.println("No TCS34725 found ... check your connections");\nwhile (1);\n}';
    generator.setups_['setup_DFRobot_TCS34725'] = 'tcs34725.begin();';

    var RGB = this.getFieldValue('DF_TCS34725_COLOR');
    return [RGB, generator.ORDER_ATOMIC];
};

///////////////////////////手势识别传感器/////////////////
//传感器-手势识别-获取数据
export const ke_Gesture_APDS = function (_, generator) {
    generator.definitions_['include_SparkFun_APDS9960'] = '#include "SparkFun_APDS9960.h"';
    generator.definitions_['var_APDS'] = 'SparkFun_APDS9960 apds;';
    generator.setups_['setup_APDS'] = 'apds.init();';
    generator.setups_['setup_APDS1'] = 'apds.enableGestureSensor(true);';
    var code = '';
    code += 'apds.handleGesture()';
    return [code, generator.ORDER_ATOMIC];
};

////////////////////////////////////////////////////
//////////////////////////显示屏///////////////////
//////////////////////////////////////////////////////////

///////////////////////////RGB灯/////////////////
export const ke_rgb2812 = function (_, generator) {
    var dropdown_rgbpin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var value_led = generator.valueToCode(this, 'num01', generator.ORDER_ATOMIC);
    var value_rvalue = generator.valueToCode(this, 'red', generator.ORDER_ATOMIC);
    var value_gvalue = generator.valueToCode(this, 'green', generator.ORDER_ATOMIC);
    var value_bvalue = generator.valueToCode(this, 'blue', generator.ORDER_ATOMIC);
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_Adafruit_NeoPixel'] = '#include <Adafruit_NeoPixel.h>';
    if (!generator.definitions_['var_rgb_display' + dropdown_rgbpin]) {
        generator.definitions_['var_rgb_display' + dropdown_rgbpin] = 'Adafruit_NeoPixel  rgb_display_' + dropdown_rgbpin + '' + '(8);';
        generator.setups_['setup_rgb_display_begin_' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.begin();';
        generator.setups_['setup_rgb_display_setpin' + dropdown_rgbpin] = 'rgb_display_' + dropdown_rgbpin + '.setPin(' + dropdown_rgbpin + ');';
    }

    var code = 'rgb_display_' + dropdown_rgbpin + '.setPixelColor(' + value_led + '-1, ' + value_rvalue + ',' + value_gvalue + ',' + value_bvalue + ');\n';
    code += 'rgb_display_' + dropdown_rgbpin + '.show();\n';
    return code;
};

//TM1650 4 digital display  -- 初始化
export const ke_4digitdisplay_init = function (_, generator) {
    var CLK = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC || '10');
    var DIO = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC || '11');
    generator.definitions_['include_TM1650'] = '#include "KETM1650.h"';
    generator.definitions_['var_display_4display'] = 'KETM1650 tm_4display(' + CLK + ',' + DIO + ');';
    generator.setups_['setup_tm1637_init'] = 'tm_4display.init();\n';
    return '';
};

// TM1650 4 digital display  -- 开关 清屏
export const ke_4digitdisplay_power = function () {
    var stat = this.getFieldValue("STAT");
    return 'tm_4display.' + stat + '();\n';
}

// TM1650 4 digital display  -- 显示字符串
export const ke_4digitdisplay_displayString = function (_, generator) {
    // var value = generator.valueToCode(this, 'VALUE', generator.ORDER_ATOMIC);
    var value = generator.valueToCode(this, 'VALUE', generator.ORDER_ATOMIC) || '""';
    return 'tm_4display.displayString(' + value + ');\n';
}

// TM1650 4 digital display  -- 显示数字
export const ke_4digitdisplay_displayNumber = function (_, generator) {
    // var pos = generator.valueToCode(this, 'POS', generator.ORDER_ATOMIC);
    var value = generator.valueToCode(this, 'VALUE', generator.ORDER_ATOMIC);
    // generator.definitions_['value_buf'] = 'char buf[5];';
    // return 'snprintf_P(buf, sizeof(buf), PSTR("%u"), '+value+');\n'
    //  + 'tm_4display.displayString(buf);\n';
    return 'tm_4display.displayString(' + value + ');\n';
}

// TM1650 4 digital display  -- 小数点显示
export const ke_4digitdisplay_showDot = function (_, generator) {
    //var no=this.getFieldValue("NO");
    var dotNum = generator.valueToCode(this, 'DOTNUM', generator.ORDER_ASSIGNMENT) || '2';
    var stat = this.getFieldValue("STAT");
    return 'tm_4display.setDot(' + dotNum + '-1 ,' + stat + ');\n';
}

// TM1650 4 digital display  -- 设置亮度
export const ke_4digitdisplay_Brightness = function (_, generator) {
    var brightness = generator.valueToCode(this, 'brightness', generator.ORDER_ASSIGNMENT) || '8';
    var code = 'tm_4display.setBrightness(' + brightness + ');\n';
    return code;
};

// TM1650 4 digital display  -- 时钟模式
export const ke_4digitdisplay_Time = function (_, generator) {
    var time_hour = generator.valueToCode(this, 'time_hour', generator.ORDER_ASSIGNMENT);
    var time_minute = generator.valueToCode(this, 'time_minute', generator.ORDER_ASSIGNMENT);
    var time_second = this.getFieldValue('STAT');
    generator.definitions_['value_buf'] = 'char buf[5];';
    var code = 'snprintf_P(buf, sizeof(buf), PSTR("%02u%02u"), ' + time_hour + ', ' + time_minute + ');\n'
        + 'tm_4display.displayString(buf);\n';
    if (time_second == 'displaySEC_BLINK') {
        code += 'tm_4display.setDot(1,true);\n' + 'delay(500);\n' + 'tm_4display.setDot(1,false);\n' + 'delay(500);\n';
    } else if (time_second == 'displaySEC_ON') {
        generator.setups_['setup_display_4display_init'] = 'tm_4display.setDot(1,true);\n';
    }
    return code;
};

/*
export const ke_display_TM1637_init = function (_, generator) {
 tm1637_CLK = this.getFieldValue('CLK');
 tm1637_DIO = this.getFieldValue('DIO');
 generator.definitions_['include_SevenSegmentTM1637'] = '#include <SevenSegmentTM1637.h>';
 generator.definitions_['var_declare_SevenSegmentTM1637'] = 'SevenSegmentTM1637  display(' + tm1637_CLK + ',' + tm1637_DIO + ');';
 generator.setups_['setup_ display.begin()'] = ' display.begin();';
 return '';
};

export const ke_display_TM1637_displyPrint = function (_, generator) {
    //var Speed = generator.valueToCode(this, 'Speed', generator.ORDER_ATOMIC);
    var VALUE = generator.valueToCode(this, 'VALUE', generator.ORDER_ATOMIC);
    var code = 'display.print(' + VALUE + ');' + '\n';
    return code;
  };

  export const ke_display_TM1637_displayTime = function (_, generator) {
    generator.definitions_['include_SevenSegmentExtended'] = '#include <SevenSegmentExtended.h>';
    generator.definitions_['var_declare_SevenSegmentTM1637'] = 'SevenSegmentExtended  display(' + tm1637_CLK + ',' + tm1637_DIO + ');';
    var hour = generator.valueToCode(this, 'hour', generator.ORDER_ATOMIC);
    var minute = generator.valueToCode(this, 'minute', generator.ORDER_ATOMIC);
    var dropdown_stat = this.getFieldValue("STAT");
    var code = 'display.printTime(' + hour + ',' + minute +','+dropdown_stat+');\n';
    return code;
  };

  export const ke_display_TM1637_clearDisplay = function (_, generator) {
    var stat=this.getFieldValue("STAT");
    return 'display.'+stat+'();\n';
  };

  export const ke_display_TM1637_Brightness = function (_, generator) {
    var BRIGHTNESS = generator.valueToCode(this, 'Brightness', generator.ORDER_ATOMIC);
    var code = 'display.setBacklight(' + BRIGHTNESS + ');\n';
    return code;
  };

////////////////////////////TM1637数码管初始化//////////////////////////
export const ke_tm1637_init = function(){
  var CLK = generator.valueToCode(this, 'PIN1',generator.ORDER_ATOMIC);
  var DIO = generator.valueToCode(this, 'PIN2',generator.ORDER_ATOMIC);

  generator.definitions_['include_TM1637.h'] = '#include <TM1637.h>';
  generator.definitions_['TM1637'] = 'TM1637 TM('+CLK+', '+DIO+');';
  return '';
};

///////////////////////////TM1637数码管自定义显示/////////////////
export const ke_tm1637_dy = function(){
  var num = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC);
  //var tc = generator.valueToCode(this, 'tc', generator.ORDER_ATOMIC);
  var weishu = generator.valueToCode(this, 'weishu', generator.ORDER_ATOMIC);
  var wei = generator.valueToCode(this, 'wei', generator.ORDER_ATOMIC);
  var code = 'TM.cls();\nTM.Num('+num+',0,'+weishu+','+wei+');'+'\n';
  return code;
};

///////////////////////////TM1637数码管自定义显示是否填充/////////////////
export const ke_tm1637_tc = function(){
  var num = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC);
  //var tc = generator.valueToCode(this, 'tc', generator.ORDER_ATOMIC);
  var tc = generator.valueToCode(this, '0and1', generator.ORDER_ATOMIC);
  var code = 'TM.cls();\nTM.Num('+num+','+tc+');'+'\n';
  return code;
};

///////////////////////////TM1637数码管亮度/////////////////
export const ke_tm1637_ld = function(){
  var num = generator.valueToCode(this, 'num', generator.ORDER_ATOMIC);
  var code = 'TM.BL('+num+');'+'\n';
  return code;
};


///////////////////////////TM1637数码管是否隐藏/////////////////
export const ke_tm1637_yc = function(){
  var bool = generator.valueToCode(this, 'BOOL', generator.ORDER_ATOMIC);
  var code = 'TM.show('+bool+');'+'\n';
  return code;
};

///////////////////////////TM1637数码管是否显示冒号////////////////
export const ke_tm1637_mh = function(){
  var bool = generator.valueToCode(this, 'BOOL', generator.ORDER_ATOMIC);
  var num1 = generator.valueToCode(this, 'num1', generator.ORDER_ATOMIC);
  var num2 = generator.valueToCode(this, 'num2', generator.ORDER_ATOMIC);


  var code = 'TM.DNum('+num1+','+num2+','+bool+');'+'\n';
  return code;
};
*/


////////////////////////1602LCD无IIC///////////////////////////
export const ke_1602lcd_w = function (_, generator) {
    var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC) || 'String("")'
    var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC) || 'String("")'


    generator.definitions_['DRE'] = 'int DI = 12,RW = 11,Enable = 2;';
    generator.definitions_['DB'] = 'int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};';
    generator.definitions_['STR'] = 'char *string = "0";';
    generator.definitions_['LcdCommandWrite'] = 'void LcdCommandWrite(int value) \n{\nint i = 0;\nfor (i=DB[0]; i <= DI; i++) \n{\ndigitalWrite(i,value & 01);\nvalue >>= 1;\n}\ndigitalWrite(Enable,LOW);\ndelayMicroseconds(1);\ndigitalWrite(Enable,HIGH);\ndelayMicroseconds(1); \ndigitalWrite(Enable,LOW);\ndelayMicroseconds(1); \n}\n';
    generator.definitions_['LcdDataWrite'] = 'void LcdDataWrite(int value) \n{\nint i = 0;\ndigitalWrite(DI, HIGH); \ndigitalWrite(RW, LOW); \nfor (i=DB[0]; i <= DB[7]; i++) \n{\ndigitalWrite(i,value & 01);\nvalue >>= 1;\n}\ndigitalWrite(Enable,LOW);  \ndelayMicroseconds(1);\ndigitalWrite(Enable,HIGH); \ndelayMicroseconds(1);\ndigitalWrite(Enable,LOW); \ndelayMicroseconds(1); \n}\n';
    //generator.definitions_['LcdStringWrite'] = 'void LcdStringWrite(char *a)\n{\nfor(int i = 0;i < 14; i++)\n{\nif(*(a+i) == '\0')\n {\nbreak;\n}\nLcdDataWrite(*(a+i));\n}\n}\n';
    generator.definitions_['lsw'] = 'void LcdStringWrite(char *a)\n{\nfor(int i = 0;i < strlen(a); i++){\nLcdDataWrite(*(a+i));\n}\n}\n';

    generator.setups_['setup_lcd'] = 'int i = 0;\nfor (i=Enable; i <= DI; i++) \n{\n    pinMode(i,OUTPUT);\n}\ndelay(100);\nLcdCommandWrite(0x38);\ndelay(64);  \nLcdCommandWrite(0x38); \ndelay(50);  \nLcdCommandWrite(0x38); \ndelay(20);  \nLcdCommandWrite(0x06); \ndelay(20);  \nLcdCommandWrite(0x0E);\ndelay(20);  \nLcdCommandWrite(0x01);  \ndelay(100); \nLcdCommandWrite(0x80); \ndelay(20);  \n';

    var code = 'LcdCommandWrite(0x01); \ndelay(10); \nLcdCommandWrite(0x80+0); \ndelay(10); \nstring = ' + str1 + ';\nLcdStringWrite(string);\ndelay(10);\nLcdCommandWrite(0xc0+0); \n string = ' + str2 + ';\nLcdStringWrite(string);\ndelay(10);\ndelay(300);';

    return code;
};


export const ke_1602lcd_init = function (_, generator) {
    var varName = this.getFieldValue('VAR');
    var TYPE = this.getFieldValue('TYPE');
    var device = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC) || '0x27';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_LiquidCrystal_I2C'] = '#include <LiquidCrystal_I2C.h>';
    generator.definitions_['var_LiquidCrystal_I2C_' + varName] = 'LiquidCrystal_I2C ' + varName + '(' + device + ',' + TYPE + ');';
    return '';
};

export const ke_1602lcd = function (_, generator) {
    var varName = this.getFieldValue('VAR');
    var str1 = generator.valueToCode(this, 'TEXT', generator.ORDER_ATOMIC) || '""';
    var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC) || '""';
    //var str3 = generator.valueToCode(this, 'TEXT3', generator.ORDER_ATOMIC) || '""';
    //var str4 = generator.valueToCode(this, 'TEXT4', generator.ORDER_ATOMIC) || '""';
    if (generator.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || generator.definitions_['var_LiquidCrystal_I2C_' + varName]) {
        generator.setups_['setup_lcd_init_' + varName] = varName + '.init();';
        generator.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
    }
    var code = varName + '.setCursor(0, 0);\n'
    code += varName + '.print(' + str1 + ');\n';
    code += varName + '.setCursor(0, 1);\n';
    code += varName + '.print(' + str2 + ');\n';
    //code+=varName+'.setCursor(0, 2);\n';
    //code+=varName+'.print('+str3+');\n';
    //code+=varName+'.setCursor(0, 3);\n';
    //code+=varName+'.print('+str4+');\n';
    return code;
};

export const ke_1602lcd_print2 = function (_, generator) {
    var varName = this.getFieldValue('VAR');
    var str = generator.valueToCode(this, 'TEXT', generator.ORDER_ATOMIC) || 'String("")';
    var row = generator.valueToCode(this, 'row', generator.ORDER_ATOMIC) || '1';
    var column = generator.valueToCode(this, 'column', generator.ORDER_ATOMIC) || '1';
    if (generator.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || generator.definitions_['var_LiquidCrystal_I2C_' + varName]) {
        generator.setups_['setup_lcd_init_' + varName] = varName + '.init();';
        generator.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
    }
    var code = varName + '.setCursor(' + column + '-1, ' + row + '-1);\n'
    code += varName + '.print(' + str + ');\n';
    return code;
};

export const ke_1602lcd_left = function () {
    var varName = this.getFieldValue('VAR');
    var code = '' + varName + '.scrollDisplayLeft();\n'
    return code;
};

export const ke_1602lcd_right = function () {
    var varName = this.getFieldValue('VAR');
    var code = '' + varName + '.scrollDisplayRight();\n'
    return code;
};


////////////////////////1602LCD///////////////////////////
/*export const ke_1602lcd = function() {
  var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC) || 'String("")'
  var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC) || 'String("")'
  var dropdown_stat = this.getFieldValue('STAT');
  //var bool = generator.valueToCode(this, 'BOOL', generator.ORDER_ATOMIC);
  generator.definitions_['define_i2c'] = '#include <Wire.h>';
  generator.definitions_['define_lcd'] = '#include <LiquidCrystal_I2C.h>';
  generator.definitions_['var_lcd'] = 'LiquidCrystal_I2C lcd(0x27,16,2);';
  generator.setups_['setup_lcd1'] = 'lcd.init();';
  generator.setups_['setup_lcd2'] = 'lcd.backlight();';
  var code = 'lcd.setCursor(0, 0);\n'
  code+='lcd.print('+str1+');\n';
  code+='lcd.setCursor(0, 1);\n';
  code+='lcd.print('+str2+');\n';
  return code;
};*/

export const ke_1602lcd_clear = function (_, generator) {
    var varName = this.getFieldValue('VAR');
    var dropdown_stat = this.getFieldValue('STAT');
    if (generator.definitions_['var_LiquidCrystal_SoftI2C_' + varName] || generator.definitions_['var_LiquidCrystal_I2C_' + varName]) {
        generator.setups_['setup_lcd_init_' + varName] = varName + '.init();';
        generator.setups_['setup_lcd_backlight_' + varName] = varName + '.backlight();';
    }
    var code = varName + '.' + dropdown_stat + '();\n'
    return code;
};

////////////////////////1602LCD///////////////////////////
/*export const ke_1602lcd_clear = function() {

  var code = 'lcd.clear();\n'

  return code;
};*/


export const lcd128_init = function (_, generator) {
    //generator.includes_.include_lcd128_init = '#include <lcd.h>\n';  Mixly没有这条语句
    generator.definitions_['1lcd128_init'] = '#include <lcd.h>\nlcd Lcd;\n';
    generator.setups_['lcd128_setup'] = 'Lcd.Init();\n';
    var code = '';
    return code;
};



export const lcd128_cursor = function (_, generator) {
    var row = generator.valueToCode(this, 'row', generator.ORDER_ATOMIC);
    var col = generator.valueToCode(this, 'col', generator.ORDER_ATOMIC);

    var code = 'Lcd.Cursor(' + row + ',' + col + ');\n';
    return code;
};

export const lcd128_string = function (_, generator) {
    //var lcd128str = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC) || 'String("")'
    //const lcd128str = generator.valueToCode(block, 'string', generator.ORDER_ATOMIC);
    const lcd128str = generator.valueToCode(this, 'string', generator.ORDER_ATOMIC);
    var code = 'Lcd.Display(' + lcd128str + ');\n';
    return code;
};

export const lcd128_pixel = function (_, generator) {
    const row = generator.valueToCode(this, 'row', generator.ORDER_ATOMIC);
    const col = generator.valueToCode(this, 'col', generator.ORDER_ATOMIC);
    var code = `Lcd.DisplayPixel(${row},${col});\n`;
    return code;
};

export const lcd128_d_pixel = function (_, generator) {
    const row = generator.valueToCode(this, 'row', generator.ORDER_ATOMIC);
    const col = generator.valueToCode(this, 'col', generator.ORDER_ATOMIC);
    var code = `Lcd.ClearPixel(${row},${col});\n`
    return code;
};


export const lcd128_clear = function () {
    var code = 'Lcd.Clear();\n'
    return code;
};




////////////////////////2004LCD///////////////////////////
export const ke_2004lcd = function (_, generator) {
    var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC) || 'String("")'
    var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC) || 'String("")'
    var str3 = generator.valueToCode(this, 'TEXT3', generator.ORDER_ATOMIC) || 'String("")'
    var str4 = generator.valueToCode(this, 'TEXT4', generator.ORDER_ATOMIC) || 'String("")'

    generator.definitions_['define_i2c'] = '#include <Wire.h>';
    generator.definitions_['define_df_lcd'] = '#include <LiquidCrystal_I2C.h>';
    generator.definitions_['var_df_lcd'] = 'LiquidCrystal_I2C df_lcd(0x27,16,4);';
    generator.setups_['setup_df_lcd1'] = 'df_lcd.init();';
    generator.setups_['setup_df_lcd2'] = 'df_lcd.backlight();';
    var code = 'df_lcd.setCursor(0, 0);\n'
    code += 'df_lcd.print(' + str1 + ');\n';
    code += 'df_lcd.setCursor(0, 1);\n';
    code += 'df_lcd.print(' + str2 + ');\n';
    code += 'df_lcd.setCursor(0, 2);\n';
    code += 'df_lcd.print(' + str3 + ');\n';
    code += 'df_lcd.setCursor(0, 3);\n';
    code += 'df_lcd.print(' + str4 + ');\n';
    return code;
};

////////////////////////oled_init///////////////////////////
export const ke_OLED_init = function (_, generator) {
    generator.definitions_['define_Wire'] = '#include <Wire.h>';
    generator.definitions_['define_GFX'] = '#include <Adafruit_GFX.h>';
    generator.definitions_['define_SSD1306'] = '#include <Adafruit_SSD1306.h>';

    generator.definitions_['OLED_RESET'] = '#define OLED_RESET 4';
    generator.definitions_['display'] = 'Adafruit_SSD1306 display(OLED_RESET);';

    generator.setups_['setup_oled1'] = 'display.begin(SSD1306_SWITCHCAPVCC, 0x3C);';
    generator.setups_['setup_oled2'] = 'display.display();';
    generator.setups_['setup_oled3'] = 'delay(2000);';
    generator.setups_['setup_oled4'] = 'display.clearDisplay();';

    var code = ''

    return code;
};

////////////////////////oled_pixel///////////////////////////
export const ke_OLED_piexl = function (_, generator) {
    var valx = generator.valueToCode(this, 'val_x', generator.ORDER_ATOMIC);
    var valy = generator.valueToCode(this, 'val_y', generator.ORDER_ATOMIC);

    var code = 'display.drawPixel(' + valx + ', ' + valy + ', WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_line///////////////////////////
export const ke_OLED_line = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_x2', generator.ORDER_ATOMIC);
    var valy2 = generator.valueToCode(this, 'val_y2', generator.ORDER_ATOMIC);

    var code = 'display.drawLine(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', ' + valy2 + ', WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_drawRect///////////////////////////
export const ke_OLED_rect = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_x2', generator.ORDER_ATOMIC);
    var valy2 = generator.valueToCode(this, 'val_y2', generator.ORDER_ATOMIC);

    var code = 'display.drawRect(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', ' + valy2 + ', WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_drawfullRect///////////////////////////
export const ke_OLED_fullrect = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_x2', generator.ORDER_ATOMIC);
    var valy2 = generator.valueToCode(this, 'val_y2', generator.ORDER_ATOMIC);

    var code = 'display.fillRect(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', ' + valy2 + ', 1);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_roundrect///////////////////////////
export const ke_OLED_roundrect = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_x2', generator.ORDER_ATOMIC);
    var valy2 = generator.valueToCode(this, 'val_y2', generator.ORDER_ATOMIC);
    var val_round = generator.valueToCode(this, 'val_round', generator.ORDER_ATOMIC);

    var code = 'display.drawRoundRect(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', ' + valy2 + ', ' + val_round + ',  WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_fullroundrect///////////////////////////
export const ke_OLED_fullroundrect = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_x2', generator.ORDER_ATOMIC);
    var valy2 = generator.valueToCode(this, 'val_y2', generator.ORDER_ATOMIC);
    var val_round = generator.valueToCode(this, 'val_round', generator.ORDER_ATOMIC);

    var code = 'display.fillRoundRect(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', ' + valy2 + ', ' + val_round + ',  WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_drawcircle///////////////////////////
export const ke_OLED_circle = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_circle', generator.ORDER_ATOMIC);

    var code = 'display.drawCircle(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_drawfillcircle///////////////////////////
export const ke_OLED_fullcircle = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_circle', generator.ORDER_ATOMIC);

    var code = 'display.fillCircle(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_triangle///////////////////////////
export const ke_OLED_triangle = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_x2', generator.ORDER_ATOMIC);
    var valy2 = generator.valueToCode(this, 'val_y2', generator.ORDER_ATOMIC);
    var valx3 = generator.valueToCode(this, 'val_x3', generator.ORDER_ATOMIC);
    var valy3 = generator.valueToCode(this, 'val_y3', generator.ORDER_ATOMIC);

    var code = 'display.drawTriangle(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', ' + valy2 + ', ' + valx3 + ', ' + valy3 + ',WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_fulltriangle///////////////////////////
export const ke_OLED_fulltriangle = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var valx2 = generator.valueToCode(this, 'val_x2', generator.ORDER_ATOMIC);
    var valy2 = generator.valueToCode(this, 'val_y2', generator.ORDER_ATOMIC);
    var valx3 = generator.valueToCode(this, 'val_x3', generator.ORDER_ATOMIC);
    var valy3 = generator.valueToCode(this, 'val_y3', generator.ORDER_ATOMIC);

    var code = 'display.fillTriangle(' + valx1 + ', ' + valy1 + ', ' + valx2 + ', ' + valy2 + ', ' + valx3 + ', ' + valy3 + ',WHITE);\ndisplay.display();\n'

    return code;
};

////////////////////////oled_string///////////////////////////
export const ke_OLED_string = function (_, generator) {
    var valx1 = generator.valueToCode(this, 'val_x1', generator.ORDER_ATOMIC);
    var valy1 = generator.valueToCode(this, 'val_y1', generator.ORDER_ATOMIC);
    var val_size = generator.valueToCode(this, 'size', generator.ORDER_ATOMIC);
    var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC);

    var code = 'display.setTextSize(' + val_size + ');\n  display.setTextColor(WHITE);\n  display.setCursor(' + valx1 + ',' + valy1 + ');\ndisplay.println(' + str1 + ');\ndisplay.display();\n'

    return code;
};



//////////////////////////OLED//////////////////////////////////////
export const ke_oled = function (_, generator) {

    var s = generator.valueToCode(this, 'size', generator.ORDER_ATOMIC);

    var str1 = generator.valueToCode(this, 'TEXT1', generator.ORDER_ATOMIC);
    var str2 = generator.valueToCode(this, 'TEXT2', generator.ORDER_ATOMIC);
    var str3 = generator.valueToCode(this, 'TEXT3', generator.ORDER_ATOMIC);
    var str4 = generator.valueToCode(this, 'TEXT4', generator.ORDER_ATOMIC);
    var code = 'display.setTextSize(' + s + ');\ndisplay.setTextColor(WHITE);\ndisplay.setCursor(0,0);\ndisplay.println(' + str1 + ');\ndisplay.println(' + str2 + ');\ndisplay.println(' + str3 + ');\ndisplay.println(' + str4 + ');\ndisplay.display();\ndelay(250);';
    return code;
};


////////////////////////oled_clear///////////////////////////
export const ke_oled_clear = function () {

    var code = 'display.clearDisplay();\n'

    return code;
};



//////////////////////////////点阵初始化/////////////////////////////
export const ke_matrix_init = function (_, generator) {
    var SDA = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var SCL = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);
    var addr = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);

    var matrixName = this.getFieldValue('matrixName');
    generator.definitions_['include_Matrix'] = '#include <Matrix.h>';
    generator.definitions_[matrixName] = 'Matrix ' + matrixName + '(' + SDA + ',' + SCL + ');';
    generator.setups_['setup_' + matrixName] = matrixName + '.begin(' + addr + '); \n';
    var code = matrixName + '.clear();\n';
    return code;
};

export const ke_Matrix_POS = function (_, generator) {
    var pos_x = generator.valueToCode(this, 'XVALUE', generator.ORDER_ASSIGNMENT);
    var pos_y = generator.valueToCode(this, 'YVALUE', generator.ORDER_ASSIGNMENT);
    var matrixName = this.getFieldValue('matrixName');
    var dropdown_type = this.getFieldValue('DrawPixel_TYPE');
    var code = matrixName + '.drawPixel(' + pos_x + '-1,' + pos_y + '-1,' + dropdown_type + ');\n'
    code += matrixName + '.write();\n';
    return code;
};
export const ke_Matrix_Rotation = function () {
    var matrixName = this.getFieldValue('matrixName');
    var dropdown_type = this.getFieldValue('Rotation_TYPE');
    var code = matrixName + '.setRotation(' + dropdown_type + ');\n'
    return code;
};
export const ke_matrix_text = function (_, generator) {
    var matrixName = this.getFieldValue('matrixName');
    var textString = generator.valueToCode(this, 'TEXT', generator.ORDER_ASSIGNMENT);
    var code = matrixName + '.drawStr(' + textString + ');\n'
    return code;
};
//执行器_点阵屏显示_显示图案
export const ke_matrix1 = function (_, generator) {
    var matrixName = this.getFieldValue('matrixName');
    var dotMatrixArray = generator.valueToCode(this, 'LEDArray', generator.ORDER_ASSIGNMENT);
    generator.definitions_['LEDArray'] = 'uint8_t  LEDArray[8];';
    //  var code='Matrix_'+SDA+'_'+SCL+'.clear()dotMatrix;\n';
    var code = '';
    code += 'for(int i=0; i<8; i++)\n';
    code += '{\n'
    code += '  LEDArray[i]=' + dotMatrixArray + '[i];\n';
    code += '  for(int j=7; j>=0; j--)\n'
    code += '  {\n'
    code += '    if((LEDArray[i]&0x01)>0)\n';
    code += '    ' + matrixName + '.drawPixel(j, i,1);\n';
    code += '    LEDArray[i] = LEDArray[i]>>1;\n';
    code += '  }  \n'
    code += '}\n'
    code += matrixName + '.write();\n'
    return code;
};
//执行器_点阵屏显示_点阵数组
export const ke_matrix2 = function (_, generator) {
    var varName = this.getFieldValue('VAR');
    var a = new Array();
    for (let i = 1; i < 9; i++) {
        a[i] = new Array();
        for (let j = 1; j < 9; j++) {
            a[i][j] = (this.getFieldValue('a' + i + j) == "TRUE") ? 1 : 0;
        }
    }
    var code = '{';
    for (let i = 1; i < 9; i++) {
        var tmp = ""
        for (let j = 1; j < 9; j++) {
            tmp += a[i][j];
        }
        tmp = (parseInt(tmp, 2)).toString(16)
        if (tmp.length == 1) tmp = "0" + tmp;
        code += '0x' + tmp + ((i != 8) ? ',' : '');
    }
    code += '};';
    //generator.definitions_[this.id] = "byte LedArray_"+clearString(this.id)+"[]="+code;
    generator.definitions_[varName] = "uint8_t " + varName + "[8]=" + code;
    //return ["LedArray_"+clearString(this.id), generator.ORDER_ATOMIC];
    return [varName, generator.ORDER_ATOMIC];
};


////////////////////////清屏/////////////////////////////
export const ke_matrix_clear = function () {
    var matrixName = this.getFieldValue('matrixName');
    var code = matrixName + '.clear();\n'
    code += matrixName + '.write();\n';
    return code;
};


////////////////////////////////点阵1////////////////////////////
//执行器_点阵屏显示_显示图案
/*export const ke_matrix1 = function() {
  var matrixName = this.getFieldValue('matrixName');
  var dotMatrixArray = generator.valueToCode(this, 'LEDArray', generator.ORDER_ASSIGNMENT);
  generator.definitions_['LEDArray'] = 'uint8_t  LEDArray[8];';
//  var code='Matrix_'+SDA+'_'+SCL+'.clear()dotMatrix;\n';
  var code='';
  code+='for(int i=0; i<8; i++)\n';
  code+='{\n'       //由于硬件横向引脚排序往左移了一位的问题，需要程序将数组往右移一位。
  code+='if('+dotMatrixArray+'[i] & 0x01)\n'   //如果数据最后一位是1，1是有效数值
  code+='{\n'
  code+='LEDArray[i]=('+dotMatrixArray+'[i] >> 1) | 0x80;\n' //这里先整体往右移了，就是把最后一个数（1）移掉了，就需要或上0x80,相当于把1放到前面去。
  code+='}\n'
  code+='else\n'    //如果最后一位是0
  code+='{'
  code+='LEDArray[i]='+dotMatrixArray+'[i]>>1;\n'   //就直接往右移一位就可以，前面会自动补0
  code+='}';
  //code+='  for(int j=7; j>=0; j--)\n'
  code+='  for(int j=0; j<=7; j++)\n'
  code+='  {\n'
  code+='    if((LEDArray[i]&0x01)>0)\n';
  code+='    '+ matrixName +'.drawPixel(j, i,1);\n';
  code+='    LEDArray[i] = LEDArray[i]>>1;\n';
  code+='  }  \n'
  code+='}\n'
  code+= matrixName+'.writeDisplay();\n'
  return code;
};
//执行器_点阵屏显示_点阵数组
export const ke_matrix2 = function() {
  var varName = this.getFieldValue('VAR');
  var a = new Array();
  for (var i = 1; i < 9; i++) {
    a[i] = new Array();
    for (var j = 1; j < 9; j++) {
      a[i][j] = (this.getFieldValue('a' + i + j) == "TRUE") ? 1 : 0;
    }
  }
  var code = '{';
  for (var i = 1; i < 9; i++) {
    var tmp = ""
    for (var j = 1; j < 9; j++) {
      tmp += a[i][j];

    }
    tmp = (parseInt(tmp, 2)).toString(16)
    if (tmp.length == 1) tmp = "0" + tmp;
    code += '0x' + tmp + ((i != 8) ? ',' : '');
  }
  code += '};';
  //generator.definitions_[this.id] = "byte LedArray_"+clearString(this.id)+"[]="+code;
  generator.definitions_[varName] = "uint8_t " + varName + "[8]=" + code;
  //return ["LedArray_"+clearString(this.id), generator.ORDER_ATOMIC];
  return [varName, generator.ORDER_ATOMIC];
};*/




////////////////////////////////通讯///////////////////////////////

///////////////////////红外接收///////////////////
export const ke_ir_r = function (_, generator) {
    var variable = generator.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
    generator.definitions_['var_declare' + variable] = 'long ' + variable + ';';
    var dropdown_pin = generator.valueToCode(this, 'PIN', generator.ORDER_ATOMIC);
    var branch = generator.statementToCode(this, 'DO');
    var branch2 = generator.statementToCode(this, 'DO2');
    generator.definitions_['include_IRremote'] = '#include <IRremote.h>\n';
    //generator.definitions_['var_declare'+varName] = 'long '+varName+';\n';
    generator.definitions_['var_ir_recv' + dropdown_pin] = 'IRrecv irrecv_' + dropdown_pin + '(' + dropdown_pin + ');\ndecode_results results_' + dropdown_pin + ';\n';
    generator.setups_['setup_ir_recv_' + dropdown_pin] = 'irrecv_' + dropdown_pin + '.enableIRIn();';
    var code = "if (irrecv_" + dropdown_pin + ".decode(&results_" + dropdown_pin + ")) {\n"
    code += '  ' + variable + '=results_' + dropdown_pin + '.value;\n';
    code += '  String type="UNKNOWN";\n';
    ////////////////////////////////////////////////////////////////
    code += '  String typelist[14]={"UNKNOWN", "NEC", "SONY", "RC5", "RC6", "DISH", "SHARP", "PANASONIC", "JVC", "SANYO", "MITSUBISHI", "SAMSUNG", "LG", "WHYNTER"};\n';
    code += '  if(results_' + dropdown_pin + '.decode_type>=1&&results_' + dropdown_pin + '.decode_type<=13){\n';
    code += '    type=typelist[results_' + dropdown_pin + '.decode_type];\n'
    code += '  }\n';
    code += '  Serial.print("IR TYPE:"+type+"  ");\n';
    code += branch;
    code += '  irrecv_' + dropdown_pin + '.resume();\n'
    code += '} else {\n';
    code += branch2;
    code += '}\n';
    return code;
};

//////////////////////////////////////红外发射/////////////////////////////
export const ke_ir_s = function (_, generator) {

    var num1 = generator.valueToCode(this, 'num1', generator.ORDER_ATOMIC);

    generator.definitions_['define_IRre.h'] = '#include <IRremote.h>';
    generator.definitions_['define_IRsend'] = 'IRsend irsend;';

    var code = 'irsend.sendRC5(' + num1 + ', 8);\ndelay(200);\n';
    return code;
};


//////////////////////RFID////////////////////////////
export const rc522_i2c_init = function (_, generator) {
    generator.definitions_['1include_rc522_iic_init'] = '#include <Wire.h>\n#include <MFRC522_I2C.h>\nMFRC522 mfrc522(0x28);\nString rfid_str = "";\n';
    generator.definitions_['1include_rc522_iic_data'] = 'String return_rfid_data()\n' +
        '{\n' +
        '  if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {\n' +
        '    delay(50);\n' +
        '    return "0";\n' +
        '  }\n' +
        '  rfid_str = "";\n' +
        '  for (byte i = 0; i < mfrc522.uid.size; i++) {\n' +
        '    rfid_str = rfid_str + String(mfrc522.uid.uidByte[i],HEX);\n' +
        '  }\n' +
        '  return rfid_str;\n' +
        '}\n';

    generator.setups_['1setup_rc522_iic'] = 'Wire.begin();\nmfrc522.PCD_Init();\n';

    return '';
};


export const rc522_i2c_read = function (_, generator) {
    return ['return_rfid_data()', generator.ORDER_ATOMIC];
};


/////////////////////////////////////蓝牙////////////////////////////////////
export const ke_bluetooth = function (_, generator) {
    var val = this.getFieldValue('VAL');
    var branch = generator.statementToCode(this, 'DO');
    var dropdown_pin1 = generator.valueToCode(this, 'PIN1', generator.ORDER_ATOMIC);
    var dropdown_pin2 = generator.valueToCode(this, 'PIN2', generator.ORDER_ATOMIC);


    generator.definitions_['include_Soft'] = '#include <SoftwareSerial.h>\n';
    generator.definitions_['mySerial'] = 'SoftwareSerial mySerial(' + dropdown_pin1 + ', ' + dropdown_pin2 + ');\n';
    generator.definitions_['char'] = 'char ' + val + ';\n';

    generator.setups_['mySerial23'] = 'mySerial.begin(9600);';

    var code = 'if (mySerial.available())\n{\n' + val + ' = mySerial.read();\n';
    code += branch;
    code += '}\n';
    return code;
};